﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Dazaza.UI.Mvc.Controllers.Helper
{
    public static class HTMLHelper
    {
        /// <summary>
        /// 把 note 转化成一定字数的简介
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        public static string ConvertNoteToBrief(string note,int charCount)
        {
            string strWithoutHtml = GetStringWithoutHtml(note);
            if (strWithoutHtml.Length > charCount)
            {
                return string.Format("{0} ...",strWithoutHtml.Substring(0,charCount));
            }
            return strWithoutHtml;
        }

        /// <summary>
        /// 过滤html，只返回文本
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string GetStringWithoutHtml(string s)
        {
            return GetStringWithoutHtml(s, true);
        }

        public static string GetStringWithoutHtml(string s, bool isRemoveUnVisibileChar)
        {
            return GetStringWithoutHtml(s, isRemoveUnVisibileChar, true);
        }

        public static string GetStringWithoutHtml(string s, bool isRemoveUnVisibileChar, bool isRemoveHtmlSpace)
        {
            s = Regex.Replace(s, "<.*?>", "");
            if (isRemoveUnVisibileChar)
                s = Regex.Replace(s, @"[\s]", "");
            if (isRemoveHtmlSpace)
            {
                s = Regex.Replace(s, @"&(quot|#34);", "", RegexOptions.IgnoreCase);
                s = Regex.Replace(s, @"&(amp|#38);", "", RegexOptions.IgnoreCase);
                s = Regex.Replace(s, @"&(lt|#60);", "", RegexOptions.IgnoreCase);
                s = Regex.Replace(s, @"&(gt|#62);", "", RegexOptions.IgnoreCase);
                s = Regex.Replace(s, @"&(nbsp|#160);", "", RegexOptions.IgnoreCase);
            }
            return s;
        }
    }
}